using System;
using System.Collections.Generic;
using System.Text;

using Wayloop.Fusion.Core.Objects;
using Wayloop.Fusion.Web.UI;

namespace Wayloop.Fusion.WebClient
{
    public partial class DefaultPage : FusionPage
    {
        public DefaultPage()
        {
            Init += DefaultPage_Init;
        }


        private void DefaultPage_Init(object sender, EventArgs e)
        {
            //Client newClient = new Client();
            //newClient.Name = "Second Client";
            //newClient.Description = "Brief description of Second Client";
            //
            //newClient = DataAccess.Client.Save(newClient);
            //DataAccess.Client.CommitChanges();

            Client c = DataContext.Client.GetById(1);
            c.Description = "Awesome client";

            DataContext.Client.SaveOrUpdate(c);
            DataContext.Client.CommitChanges();

            StringBuilder result = new StringBuilder();
            IList<Client> clients = DataContext.Client.GetAll();

            foreach (Client client in clients)
            {
                result.AppendLine(String.Format("CLIENT"));
                result.AppendLine(String.Format("Id               = {0}", client.Id));
                result.AppendLine(String.Format("Name             = {0}", client.Name));
                result.AppendLine(String.Format("Description      = {0}", client.Description));

                result.AppendLine(String.Format("Projects"));
                result.AppendLine(String.Format("("));

                foreach (Project project in client.Projects)
                {
                    result.AppendLine(String.Format("    PROJECT"));
                    result.AppendLine(String.Format("    Id           = {0}", project.Id));
                    result.AppendLine(String.Format("    Name         = {0}", project.Name));
                    result.AppendLine(String.Format("    Description  = {0}", project.Description));

                    result.AppendLine(String.Format("    Releases"));
                    result.AppendLine(String.Format("    ("));

                    foreach (ProjectRelease release in project.Releases)
                    {
                        result.AppendLine(String.Format("        RELEASE"));
                        result.AppendLine(String.Format("        Id          = {0}", release.Id));
                        result.AppendLine(String.Format("        Name        = {0}", release.Name));
                        result.AppendLine(String.Format("        Description = {0}", release.Description));
                        result.AppendLine(String.Format("        Type        = {0}", release.ReleaseDate));

                        result.AppendLine(String.Format("        Tasks"));
                        result.AppendLine(String.Format("        ("));

                        foreach (Task task in release.Tasks)
                        {
                            result.AppendLine(String.Format("            TASK"));
                            result.AppendLine(String.Format("            Id          = {0}", task.Id));
                            result.AppendLine(String.Format("            Summary     = {0}", task.Summary));
                            result.AppendLine(String.Format("            Description = {0}", task.Description));
                            result.AppendLine(String.Format("            Type        = {0}", task.Type.Name));
                            result.AppendLine(String.Format("            Priority    = {0}", task.Priority.Name));
                            result.AppendLine(String.Format("            Status      = {0}", task.Status));
                            result.AppendLine(String.Format("            Resolution  = {0}", task.Resolution));
                            result.AppendLine();
                        }

                        result.AppendLine(String.Format("        )"));
                        result.AppendLine();
                    }

                    result.AppendLine(String.Format("    )"));
                    result.AppendLine(String.Format("    Tasks"));
                    result.AppendLine(String.Format("    ("));

                    foreach (Task task in project.Tasks)
                    {
                        result.AppendLine(String.Format("        TASK"));
                        result.AppendLine(String.Format("        Id          = {0}", task.Id));
                        result.AppendLine(String.Format("        Summary     = {0}", task.Summary));
                        result.AppendLine(String.Format("        Description = {0}", task.Description));
                        result.AppendLine(String.Format("        Type        = {0}", task.Type.Name));
                        result.AppendLine(String.Format("        Priority    = {0}", task.Priority.Name));
                        result.AppendLine(String.Format("        Status      = {0}", task.Status));
                        result.AppendLine(String.Format("        Resolution  = {0}", task.Resolution));
                        result.AppendLine();
                    }

                    result.AppendLine(String.Format("    )"));
                    result.AppendLine();
                }

                result.AppendLine(String.Format(")"));
                result.AppendLine(String.Format("Users"));
                result.AppendLine(String.Format("("));

                foreach (User user in client.Users)
                {
                    result.AppendLine(String.Format("    Id           = {0}", user.Id));
                    result.AppendLine(String.Format("    Username     = {0}", user.Username));
                    result.AppendLine(String.Format("    EmailAddress = {0}", user.EmailAddress));
                    result.AppendLine(String.Format("    FirstName    = {0}", user.FirstName));
                    result.AppendLine(String.Format("    LastName     = {0}", user.LastName));
                    result.AppendLine(String.Format("    UtcOffset    = {0}", user.UtcOffset));
                    result.AppendLine();
                }

                result.AppendLine(String.Format(")"));
                result.AppendLine();
            }

            debug.InnerText = result.ToString();

            Response.Write(DataContext.User.GetByUsername("thomasjo").OwnedTasks.Count);
        }
    }
}